#include "COptionsState.h"
#include "../Managers/VIN_SoundManager.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "CMainMenuState.h"
#include "../Core/CGame.h"
#include "../Visual/VIN_BitMapFont.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../tinyxml/tinyxml.h"
#include "../Managers/InputManager.h"



COptionsMenu::COptionsMenu()
{
	m_nSelection = 0;
	m_nCursor = -1;
	m_nBGM = 1.0f;
	m_nSFX = 1.0f;
	bucket = 0;
	CursorID = CMainMenuState::GetInstance()->GetCursorID();
	winToggle = CSGD_Direct3D::GetInstance()->GetPresentParams()->Windowed;
	m_nBGI = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/Scroll.png");
	castle = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/castle.png");
	background = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/background.png");
	BGMBar = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/BGMBar.png");
	SFXBar = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/SFXBar.png");
}

COptionsMenu::~COptionsMenu()
{

}

COptionsMenu* COptionsMenu::GetInstance(void)
{
	static COptionsMenu instance;
	return &instance;
}

void COptionsMenu::Enter()
{
	//VIN_SoundManager::GetInstance()->PlayBGM(BGM_OTHERMENUS);
	LoadDefaults();
}

void COptionsMenu::Exit()
{
	m_nSelection = 0;
	Save( "resources/XML/Options.xml" );
	//VIN_SoundManager::GetInstance()->StopSound(BGM_OTHERMENUS);

}

void COptionsMenu::Render()
{
	VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pTM->Draw(background,0,0);
	pTM->Draw(castle, 530,145, 1.5f, 1.5f);
	pTM->Draw(m_nBGI,0,-1050, 2.0f, 2.0f);
	pTM->Draw(SFXBar,300,250, m_nSFX, .7f);
	pTM->Draw(BGMBar,300,390, m_nBGM, .7f);
	
	switch(m_nSelection)
	{
	case 0:
		{	
			pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 190, 180, 1.5f, 1.5f);
		}
		break;
	case 1:
		{
			pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 185, 325, 1.5f, 1.5f);
		}
		break;
	case 2:
		{
			pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 125, 450, 1.5f, 1.5f);
		}
		break;
		case 3:
		{
			pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 280, 525, 1.5f, 1.5f);
		}
		break;

	}
}

void COptionsMenu::Update(float fElapsedTime)
{
	VIN_SoundManager::GetInstance()->SetBGMVolume(m_nBGM);
	VIN_SoundManager::GetInstance()->SetSFXVolume(m_nSFX);
	bucket+= fElapsedTime;
}

bool COptionsMenu::Input()
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	if( InputManager::GetInstance()->MoveUp())
	{
				VIN_SoundManager::GetInstance()->PlayMenuSelect();

		m_nSelection--;
		if( m_nSelection == -1 )
			m_nSelection = 3;
	}

	if( InputManager::GetInstance()->MoveDown())
	{
				VIN_SoundManager::GetInstance()->PlayMenuSelect();

		m_nSelection++;
		if( m_nSelection == 4 )
			m_nSelection = 0;
	}

	if( InputManager::GetInstance()->Enter())
	{
		if(m_nSelection == 3)
		{
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();

			CMainMenuState::GetInstance()->main_scrolling = true;
			CGame::GetInstance()->ChangeState( CMainMenuState::GetInstance() );
		}
		if(m_nSelection == 2)
		{
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();

			winToggle = !winToggle;
			CSGD_Direct3D::GetInstance()->ChangeDisplayParam(CSGD_Direct3D::GetInstance()->GetPresentParams()->BackBufferWidth, 
				CSGD_Direct3D::GetInstance()->GetPresentParams()->BackBufferHeight,winToggle);
		}
	}

	if( InputManager::GetInstance()->Escape())
	{
			CGame::GetInstance()->ChangeState( CMainMenuState::GetInstance() );
			CMainMenuState::GetInstance()->main_scrolling = true;
	}

	if( InputManager::GetInstance()->MoveLeft())
	{
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();

		if( m_nSelection == 0 )
		{
			if(bucket >=.1f)
			{
				m_nSFX-= 0.05f;
				VIN_SoundManager::GetInstance()->PlayMenuConfirm();
				if( m_nSFX < 0 )
				{
					m_nSFX=0;
				}
				bucket = 0;
			}
		}
		else if( m_nSelection == 1 )
		{
			if(bucket >=.1f)
			{
				m_nBGM-= 0.05f;
				if( m_nBGM < 0 )
				{
					m_nBGM = 0;
				}
				bucket = 0;
			}
		}
	}
	if( InputManager::GetInstance()->MoveRight())
	{
		VIN_SoundManager::GetInstance()->PlayMenuConfirm();
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();
		
		if( m_nSelection == 0 )
		{
			if(bucket >=.1f)
			{
				m_nSFX+=0.05f;
				VIN_SoundManager::GetInstance()->PlayMenuConfirm();
				if( m_nSFX > 1.0f )
				{
					m_nSFX = 1.0f;
				}
				bucket = 0;
			}
		}
		else if( m_nSelection == 1 )
		{
			if(bucket >=.1f)
			{
				m_nBGM+=0.05f;	
				if( m_nBGM > 1.0f )
				{
					m_nBGM = 1.0f;
				}
				bucket = 0;
			}
		}
	}
	return true;
}

void COptionsMenu::LoadDefaults()
{
	TiXmlDocument doc;

	if( doc.LoadFile("resources/XML/Options.xml") == false)
		return;

	TiXmlElement* Root = doc.RootElement();
	if( Root == NULL )
		return;

	TiXmlElement* pOptions = Root->FirstChildElement("Option");
	double Music;
	double SFX;
	//bool WindowStatus;

	pOptions->Attribute("Music", &Music);
	pOptions->Attribute("SFX", &SFX );

	m_nBGM = (float)Music *.01f;
	m_nSFX = (float)SFX *.01f;
	//winToggle = WindowStatus;
}


bool COptionsMenu::Save(const char* filename)
{
	TiXmlDocument doc;

	TiXmlDeclaration* pDec = new TiXmlDeclaration("1.0", "utf-8", "");

	// 1. Cut a hole in the box
	// 2. Put your dec in the box
	doc.LinkEndChild(pDec);

	TiXmlElement* pRoot = new TiXmlElement("Options");

	doc.LinkEndChild(pRoot);

		TiXmlElement* pOptions = new TiXmlElement("Option");

		pOptions->SetAttribute("Music", (int)(m_nBGM*100));

		pOptions->SetAttribute("SFX", (int)(m_nSFX*100));

		// put Option in Options
		pRoot->LinkEndChild(pOptions);

	if(doc.SaveFile(filename) == false)
		return false;

	return true;
}